require "scholarcheck/version"
require "net/http"
require "net/https"
require "uri"
require "json"


module ScholarCheck

  class ScholarCheck
    def initialize(api_token)
      @api_token = api_token
      @base_url = 'https://app.scholarcheck.io/api/v1/'
    end

    def valid?(email)
      json = api_call('email/' + email)

      json['valid']
    end

    def institution(email)
      json = api_call('email/' + email)

      json['institutionName']
    end

    def api_call(endpoint)
      uri = URI(@base_url + endpoint)

      response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
        request = Net::HTTP::Get.new uri
        request.add_field("Token", @api_token)

        http.request request
      end

      JSON.parse(response.body)
    end

  end
end

